package modelo.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import modelo.OrdenCobranza;

public class DAOOrdenesCobranza {
	protected HashMap<Integer,OrdenCobranza> ordenes = new HashMap<Integer, OrdenCobranza>();
	
	public void cargarOrdenesCobranza() {
		ResultSet rs = Servidor.consultar(
			"SELECT *\n" +
			"FROM orden_cobranza\n"
			);
		
		try {
			while (rs.next()) {
				OrdenCobranza e = new OrdenCobranza(rs.getInt("numero"), rs.getDate("fecha"), rs.getDouble("monto"), rs.getDouble("impuesto"));
				ordenes.put(e.clave(), e);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<OrdenCobranza> consultarOrdenesCobranza() {
		ResultSet rs = Servidor.consultar(
			"SELECT numero\n" +
			"FROM orden_cobranza\n"
			);
		
		return adjuntarOrdenCobranza(rs);
	}

	public OrdenCobranza consultarOrdenCobranzaPorCodigo(int codigo) {
		return ordenes.get(codigo);
	}
	
	private ArrayList<OrdenCobranza> adjuntarOrdenCobranza(ResultSet rs) {
		ArrayList<OrdenCobranza> lst = new ArrayList<OrdenCobranza>();
		
		try {
			while (rs.next())
				lst.add(ordenes.get(rs.getInt("numero")));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return lst;
	}
	
}
